package model.common.root;

import java.util.ArrayList;
import java.util.List;

public abstract class Element {
	
	// Attributes
	/** Every Element has the inherent capability of owning other Elements. When an Element is removed from a model, all its
	ownedElements are also necessarily removed from the model. The abstract syntax for each kind of Element specifies what other
	kind of Elements it may own. Every Element in a model must be owned by exactly one other Element of that model, with the
	exception of the top-level Packages of the model */
	private Element owner;
	
	private List<Element> ownedElements;
	
	private List<Comment> comments;
	
	private List<Relationship> relationships;
	
	// Constructors
	public Element() {
		owner = null;
	}
	
	public Element(Element owner) {
		setOwner(owner); 
	}

	// Methods related to comments
	public void addComment(Comment comment) {
		if (comments != null) {
			comments = new ArrayList<Comment>();
		}
		if (!comments.contains(comment)) {
			comments.add(comment);
		}
	}
	
	public boolean removeComment(Comment comment) {
		return comments != null ? comments.remove(comment) : false;
	}
	
	public List<Comment> getComments() {
		if (comments == null) {
			comments = new ArrayList<Comment>();
		}
		return new ArrayList<Comment>(comments); 
	}

	// Methods related to owner and owned elements
	public Element getOwner() {
		return owner;
	}
	
	public void setOwner(Element owner) {
		this.owner = owner;
	}
	
	public void addOwned(Element owned) {
		if (ownedElements == null) {
			ownedElements = new ArrayList<Element>();
		}
		ownedElements.add(owned);
	}
	
	public boolean removeOwnedElement(Element owned) {
		return ownedElements != null ? ownedElements.remove(owned) : false;
	}
	
	public List<Element> getOwnedElements() {
		if (ownedElements == null) {
			ownedElements = new ArrayList<Element>();
		}
		return new ArrayList<Element>(ownedElements); 
	}

	// Method related to relationships
	public void addRelationship(Relationship relationship) {
		if (relationships == null) {
			relationships = new ArrayList<Relationship>();
		}
		relationships.add(relationship);
	}
	
	public boolean removeRelationShip(Relationship relationship) {
		return relationships != null ? relationships.remove(relationship) : false;
	}
	
	public List<Relationship> getRelationships() {
		if (relationships == null) {
			relationships = new ArrayList<Relationship>();
		}
		return new ArrayList<Relationship>(relationships); 
	}

}
